package com.bianguo.android.functiondemo.presenter;

import com.bianguo.android.functiondemo.bean.BaseEntity;
import com.bianguo.android.functiondemo.bean.UserBean;
import com.bianguo.android.functiondemo.model.ReptileModel;
import com.bianguo.android.functiondemo.retrofit.BaseSubscribe;
import com.bianguo.android.functiondemo.utils.MLog;
import com.bianguo.android.functiondemo.view.IReptileView;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.ResponseBody;

public class ReptilePresenter extends BasePresenter<IReptileView, ReptileModel> {

    public ReptilePresenter(IReptileView mView) {
        super(mView);
        mModel = new ReptileModel();
    }

    public void getWebServletDemoData(int page) {
        mModel.getWebServletDemoData(page, new BaseSubscribe<ResponseBody>() {
            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    MLog.mLog("getWebServletDemoData response = " + responseBody.string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable e) {
                MLog.mLog("getWebServletDemoData error = " + e.getMessage());
            }
        });
    }

    public void login(String username, String password) {
        mModel.login(username, password, new BaseSubscribe<BaseEntity<UserBean>>() {
            @Override
            public void onNext(BaseEntity<UserBean> userBeanBaseEntity) {
                if (mReference == null) {
                    return;
                }
                int status = userBeanBaseEntity.getStatus();
                if (200 == status) {
                    getView().showViewLogin(userBeanBaseEntity.getData());
                } else {
                    getView().failedView(userBeanBaseEntity.getMsg(), true, 0);
                }
            }

            @Override
            public void onError(Throwable e) {
                MLog.mLog("login error = " + e.getMessage());
            }
        });
    }

    public void register(String phoneNumber, String password) {
        mModel.register(phoneNumber, password, new BaseSubscribe<BaseEntity<UserBean>>() {
            @Override
            public void onNext(BaseEntity<UserBean> userBeanBaseEntity) {
                if (mReference == null) {
                    return;
                }
                int status = userBeanBaseEntity.getStatus();
                if (200 == status) {
                    getView().showViewLogin(userBeanBaseEntity.getData());
                } else {
                    getView().failedView(userBeanBaseEntity.getMsg(), true, 0);
                }
            }

            @Override
            public void onError(Throwable e) {
                MLog.mLog("login error = " + e.getMessage());
            }
        });
    }

    public void saveUser(String name) {
        mModel.saveUser(name, new BaseSubscribe<BaseEntity<UserBean>>() {
            @Override
            public void onNext(BaseEntity<UserBean> userBeanBaseEntity) {
                if (mReference == null) {
                    return;
                }
                int status = userBeanBaseEntity.getStatus();
                if (200 == status) {
                    getView().showViewLogin(userBeanBaseEntity.getData());
                } else {
                    getView().failedView(userBeanBaseEntity.getMsg(), true, 0);
                }
            }

            @Override
            public void onError(Throwable e) {
                MLog.mLog("login error = " + e.getMessage());
            }
        });
    }

    public void getUsers() {
        mModel.getUsers(new BaseSubscribe<BaseEntity<ArrayList<UserBean>>>() {
            @Override
            public void onNext(BaseEntity<ArrayList<UserBean>> arrayListBaseEntity) {
                ArrayList<UserBean> data = arrayListBaseEntity.getData();
                MLog.mLog("data.size = " + data.size());
            }

            @Override
            public void onError(Throwable e) {

            }
        });
    }

    public void getUserById(Integer id) {
        mModel.getUserById(id, new BaseSubscribe<BaseEntity<UserBean>>() {
            @Override
            public void onNext(BaseEntity<UserBean> userBeanBaseEntity) {
                if (mReference == null) {
                    return;
                }
                int status = userBeanBaseEntity.getStatus();
                if (200 == status) {
                    getView().showViewLogin(userBeanBaseEntity.getData());
                } else {
                    getView().failedView(userBeanBaseEntity.getMsg(), true, 0);
                }
            }

            @Override
            public void onError(Throwable e) {
                MLog.mLog("getUserById error = " + e.getMessage());
            }
        });
    }
}
